<?php
namespace app\common\server;
use x\Xbase;
class AppThree extends Xbase
{

    /**第三方余额充值
     * @param $partner 商户号
     * @param $phone 电话
     * @param $money 金额
     * @return bool
     */
    public function recharge($partner,$phone,$money){
        /* 获取商户数据 */
        $app_three = $this->db->query('select * from app_three where partner=? and status = 0',[$partner],1);
        if (!$app_three) {
            $this->setError('商户不存在');
            return false;
        }
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where phone=?',[$phone],1);
        if (!$user) {
            $this->setError('用户不存在');
            return false;
        }
        if ($app_three['three_money'] < $money) {
            $this->setError('商户押金不足');
            return false;
        }

        $this->db->begin();
        $mysql = [];
        //插入记录第三方记录
        $data = [
            'app_three_id' => $app_three['id'],
            'type' => 0,
            'before_money' => $app_three['three_money'],
            'money' => $money,
            'after_money' =>$app_three['three_money']-$money,
            'note' => '用户转入余额:'.$money."元",
            'create_time'=>time(),
        ];
        $mysql[] =$id= $this->db->insert('app_three_bill',$data);
        $d = [
            'id'=>$app_three['id'],
            'three_money'=>$data['after_money'],
        ];
        $mysql[] = $this->db->update('app_three',$d);//扣除商户押金
        //变更用户金额
        $order_no = $this->com_order_no();
        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $user['id'], 100, 'app_three_bill', $id,  $money,'money', '第三方苹果呗充值',0);
        return $this->db->end($mysql);
    }

    /**第三方积分充值
     * @param $partner 商户号
     * @param $phone 电话
     * @param $money 金额
     * @return bool
     */
    public function rechargeJf($partner,$phone,$money_jf){
        /* 获取商户数据 */
        $app_three = $this->db->query('select * from app_three where partner=? and status = 0',[$partner],1);
        if (!$app_three) {
            $this->setError('商户不存在');
            return false;
        }
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where phone=?',[$phone],1);
        if (!$user) {
            $this->setError('用户不存在');
            return false;
        }
        if ($app_three['three_money_jf'] < $money_jf) {
            $this->setError('商户押金不足');
            return false;
        }

        $this->db->begin();
        $mysql = [];
        //插入记录第三方记录
        $data = [
            'app_three_id' => $app_three['id'],
            'type' => 1,
            'before_money' => $app_three['three_money_jf'],
            'money' => $money_jf,
            'after_money' =>$app_three['three_money']-$money_jf,
            'note' => '用户转入积分:'.$money_jf,
            'create_time'=>time(),
        ];
        $mysql[] =$id= $this->db->insert('app_three_bill',$data);
        $d = [
            'id'=>$app_three['id'],
            'three_money_jf'=>$data['after_money'],
        ];
        $mysql[] = $this->db->update('app_three',$d);//扣除商户押金
        //变更用户积分
        $order_no = $this->com_order_no();
        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $user['id'], 100, 'app_three_bill', $id,  $money_jf,'money_jf', '第三方苹果呗充值',0);
        return $this->db->end($mysql);
    }

    /**
     * @param $user_id用户ID
     * @param $point 积分数量
     * @param $give_user_id 给予人ID
     */
    public function givePoint($user_id,$point,$give_user_id){
        $this->db->begin();
        $mysql = [];
        //变更用户积分
        $order_no = $this->com_order_no();
        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $user_id, 100, 'user', $give_user_id,  $point,'money_jf', '用户赠送积分',0);
        $order_no = $this->com_order_no();
        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $give_user_id, 100, 'user', $user_id,  -$point,'money_jf', '用户赠送积分',0);
        return $this->db->end($mysql);
    }

}
